package com.hooya.fa.eu.biz.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.hooya.fa.eu.biz.dao.entity.eu.his.TempXsjlAbnormal;
import com.hooya.fa.eu.biz.dao.mapper.eu.his.HisTempXsjlAbnormalMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/3/19
 * @Version 1.0.0
 */
@Slf4j
public class XsAbnormalHisListener implements ReadListener<TempXsjlAbnormal> {

    private HisTempXsjlAbnormalMapper hisTempXsjlAbnormalMapper;

    private static final int BATCH_COUNT = 100;

    private List<TempXsjlAbnormal> cacheData = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    private Date createTime;

    public XsAbnormalHisListener(HisTempXsjlAbnormalMapper hisTempXsjlAbnormalMapper) {
        this.hisTempXsjlAbnormalMapper = hisTempXsjlAbnormalMapper;
        this.createTime = new Date();
    }

    @Override
    public void invoke(TempXsjlAbnormal data, AnalysisContext context) {
        data.setCreateTime(createTime);
        cacheData.add(data);
        if (cacheData.size() >= BATCH_COUNT) {
            addTempXsjlAbnormalList();
            cacheData = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        addTempXsjlAbnormalList();
    }

    public void addTempXsjlAbnormalList() {
        if (!CollectionUtils.isEmpty(cacheData)) {
            hisTempXsjlAbnormalMapper.insertBatchSomeColumn(cacheData);
        }
    }
}
